package com.tushare.controller;

import com.tushare.entity.*;
import com.tushare.network.WapResponse;
import com.tushare.util.ServiceUtil;
import com.tushare.util.TransformClassUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
@CrossOrigin("*")
public class MarketInfoController {
    private static final String TAG = "MarketInfoController";
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    private final TransformClassUtil transformClassUtil = new TransformClassUtil();
    @Autowired
    RestTemplate restTemplate;

    /**
     * 前十大股东
     *
     * @param ts_code    股票代码
     * @param start_date 开始时间
     */
    @GetMapping("top10_holders")
    public List<Top10Holders> getTop10Holders(@RequestParam("ts_code") String ts_code,
                                              @RequestParam(value = "start_date", required = false) String start_date) {
        ServiceUtil serviceUtil = new ServiceUtil();
        serviceUtil.initWapRequest("top10_holders");
        serviceUtil.appendField("ts_code", ts_code);
        if (start_date != null) {
            serviceUtil.appendField("start_date", start_date);
        }
        WapResponse.WapData data = serviceUtil.getNetworkData(restTemplate);
        return data == null ? null : transformClassUtil.initList(data, Top10Holders.class);
    }

    /**
     * 前十大流通股东
     *
     * @param ts_code    股票代码
     * @param start_date 开始时间
     */
    @GetMapping("top10_floatholders")
    public List<Top10FloatHolders> getTop10FloatHolders(@RequestParam("ts_code") String ts_code,
                                                        @RequestParam(value = "start_date", required = false) String start_date) {
        ServiceUtil serviceUtil = new ServiceUtil();
        serviceUtil.initWapRequest("top10_floatholders");
        serviceUtil.appendField("ts_code", ts_code);
        if (start_date != null) {
            serviceUtil.appendField("start_date", start_date);
        }
        WapResponse.WapData data = serviceUtil.getNetworkData(restTemplate);
        return data == null ? null : transformClassUtil.initList(data, Top10FloatHolders.class);
    }

    /**
     * 概念股分类
     *
     * @param src 来源
     */
    @GetMapping("concept")
    public List<Concept> getConcepts(@RequestParam(value = "src", required = false) String src) {
        ServiceUtil serviceUtil = new ServiceUtil();
        serviceUtil.initWapRequest("concept");
        src = src == null ? "ts" : src;
        serviceUtil.appendField("src", src);
        WapResponse.WapData data = serviceUtil.getNetworkData(restTemplate);
        return data == null ? null : transformClassUtil.initList(data, Concept.class);
    }

    /**
     * 概念股列表
     *
     * @param ts_code 股票代码
     * @param id      概念分类ID （id来自概念股分类接口）
     */
    @GetMapping("concept_detail")
    public List<ConceptDetail> getConceptDetail(@RequestParam(value = "ts_code", required = false) String ts_code,
                                                @RequestParam(value = "id", required = false) String id) {
        ServiceUtil serviceUtil = new ServiceUtil();
        serviceUtil.initWapRequest("concept_detail");
        if (ts_code == null) {
            if (id == null) {
                return null;
            }
            serviceUtil.appendField("id", id);
        } else {
            serviceUtil.appendField("ts_code", ts_code);
        }
        WapResponse.WapData data = serviceUtil.getNetworkData(restTemplate);
        return data == null ? null : transformClassUtil.initList(data, ConceptDetail.class);
    }

    /**
     * 限售股解禁
     *
     * @param ts_code 股票代码
     */
    @GetMapping("share_float")
    public List<ShareFloat> getShareFloat(@RequestParam("ts_code") String ts_code) {
        ServiceUtil serviceUtil = new ServiceUtil();
        serviceUtil.initWapRequest("share_float");
        serviceUtil.appendField("ts_code", ts_code);
        WapResponse.WapData data = serviceUtil.getNetworkData(restTemplate);
        return data == null ? null : transformClassUtil.initList(data, ShareFloat.class);
    }

    /**
     * 股东人数
     *
     * @param ts_code 股票代码
     */
    @GetMapping("stk_holdernumber")
    public List<StkHolderNumber> getStkHolderNumber(@RequestParam("ts_code") String ts_code) {
        ServiceUtil serviceUtil = new ServiceUtil();
        serviceUtil.initWapRequest("stk_holdernumber");
        serviceUtil.appendField("ts_code", ts_code);
        WapResponse.WapData data = serviceUtil.getNetworkData(restTemplate);
        return data == null ? null : transformClassUtil.initList(data, StkHolderNumber.class);
    }

}
